Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
45 lines (39 loc) · 766 Bytes

3.1 - Coroutine.md

File metadata and controls

45 lines (39 loc) · 766 Bytes

Coroutine

短名称

2.0.132.1.0或更高版本中,增加了协程短名特性,简化了协程相关API的名称书写。可修改php.ini设置swoole.use_shortname来关闭/开启短名,默认为开启。

创建协程

go(function () {
	co::sleep(0.5);
	echo "hello";
});
go("test");
go([$object, "method"]);

通道操作

$c = new chan(1);
$c->push($data);
$c->pop();

协程客户端

$redis = new Co\Redis;
$mysql = new Co\MySQL;
$http = new Co\Http\Client;
$tcp = new Co\Client;
$http2 = new Co\Http2\Client;

其他 API

co::sleep(100);
co::fread($fp);
co::gethostbyname('www.baidu.com');

延迟执行

defer(function () use ($db) {
    $db->close();
});